﻿Imports System.IO

Module AutomaticUploadToBackYardServer
    Dim WithEvents timer As New Timers.Timer

    Public IsInit As Boolean = False

    Public files = Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "status.txt"))

    Private Sub timer_Elapsed(sender As Object, e As Timers.ElapsedEventArgs) Handles timer.Elapsed
        UploadIt()
    End Sub

    Public Function ReadStatusFile() As String
        If My.Computer.FileSystem.FileExists(files) Then
            Return My.Computer.FileSystem.ReadAllText(files)
        Else
            Return "not found yet..."
        End If
    End Function

    Public Sub InitTimer()
        If isinit = False Then
            timer = New Timers.Timer With {.Interval = 86400}
            timer.Start()
            timer.Enabled = True
            Debug.Write("timer started...")
            IsInit = True
            UploadIt()
        End If
    End Sub

    Private Sub UploadIt()
        Dim ff = UploadDatabaseFileToBackFallServer()
        Dim gg = UploadMandateFileToBackFallServer()
        Dim gpsdbs = UploadGPSBilderDBFileToBackFallServer()
        If gg Then
            My.Computer.FileSystem.WriteAllText(files, "Database Mandate upload succeed" & Date.Now.ToString & ";" & vbCrLf, True)
        Else
            My.Computer.FileSystem.WriteAllText(files, "Database Mandate  upload not succeed" & Date.Now.ToString & ";" & vbCrLf, True)
        End If
        If ff Then
            My.Computer.FileSystem.WriteAllText(files, "Database ArbAuf succeed" & Date.Now.ToString & ";" & vbCrLf, True)
        Else
            My.Computer.FileSystem.WriteAllText(files, "Database ArbAuf upload not succeed" & Date.Now.ToString & ";" & vbCrLf, True)
        End If
        If gpsdbs Then
            My.Computer.FileSystem.WriteAllText(files, " Database GPSBilder upload succeed" & Date.Now.ToString & ";" & vbCrLf, True)
        Else
            My.Computer.FileSystem.WriteAllText(files, "Database GPSBilder upload not succeed" & Date.Now.ToString & ";" & vbCrLf, True)
        End If
    End Sub
End Module
